<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="deptName" placeholder="请输入质量问题分类" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
        </div>
        <div class="head-container">
          <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false"
            :filter-node-method="filterNode" ref="tree" node-key="id" highlight-current @node-click="handleNodeClick" />
        </div>
      </el-col>
      <el-col :span="20">
        <!-- 搜索框 -->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
          <el-form-item label="质量问题分类" prop="typeNo">
            <treeselect :disabled="disabled" v-model="queryParams.typeNo" :options="deptOptions" :default-expand-level="100"
                placeholder="质量问题分类" style="width: 305px" />
          </el-form-item>

          <!-- <el-form-item label="成因" prop="questionOrigin">
            <el-input v-model="queryParams.questionOrigin" placeholder="请输入成因" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item> -->
          <el-form-item label="质量问题等级" prop="questionLevel">
            <el-select v-model="queryParams.questionLevel" placeholder="请选择质量问题等级" clearable>
              <el-option v-for="dict in dict.type.list_quality_level" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
         
          <!-- <el-form-item label="数据来源" prop="dataSource">
            <el-input v-model="queryParams.dataSource" placeholder="请输入数据来源" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item> -->
          <el-form-item style="float:right">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <!-- 操作栏 -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
              v-hasPermi="['quality:questions:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
              v-hasPermi="['quality:questions:remove']">删除</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
              v-hasPermi="['quality:questions:export']">导出</el-button>
          </el-col> -->
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <!-- 表格 -->
        <el-table v-loading="loading" :data="questionsList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="质量问题分类" align="center" prop="typeName" />
          <el-table-column label="问题描述" align="center" prop="questionInfo" />
          <!-- <el-table-column label="数据来源" align="center" prop="questionInfo" /> -->
          <!-- <el-table-column label="成因" align="center" prop="questionOrigin" /> -->
          <!-- <el-table-column label="防范措施" align="center" prop="preventiveMeasures" /> -->
          <el-table-column label="质量问题等级" align="center" prop="questionLevel">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.list_quality_level" :value="scope.row.questionLevel" />
            </template>
          </el-table-column>
          <!-- <el-table-column label="规范依据" align="center" prop="normativeBasis" />
          <el-table-column label="规范内容" align="center" prop="normativeInfo" /> -->
          <el-table-column label="数据来源" align="center" prop="dataSource">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.list_quality_question_source" :value="scope.row.dataSource" />
            </template>
          </el-table-column>
          <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
          <el-table-column label="创建时间" align="center" prop="createTime" />
          <!-- <el-table-column label="分类状态" align="center" prop="status" /> -->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-view" @click="LookUpdate(scope.row)"
                v-hasPermi="['quality:questions:edit']">查看</el-button>
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['quality:questions:edit']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['quality:questions:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      </el-col>
    </el-row>
    <!-- 添加或修改管理清单对话框 -->
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" class="dialogForm" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="质量问题分类" prop="typeNo">
              <treeselect :disabled="disabled" v-model="form.typeNo" :options="deptOptions" :default-expand-level="100"
                placeholder="质量问题分类" style="width: 305px" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="问题等级" prop="questionLevel">
              <el-select :disabled="disabled" v-model="form.questionLevel" placeholder="请选择质量问题等级" style="width: 305px">
                <el-option v-for="dict in dict.type.list_quality_level" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据来源" prop="dataSource">
              <el-select :disabled="true" v-model="form.dataSource" placeholder="请选择数据来源" style="width: 305px">
                <el-option v-for="dict in dict.type.list_quality_question_source" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="问题描述" prop="questionInfo">
          <el-input :autosize="{ minRows: 1, maxRows: 10 }" maxlength="255" type="textarea" :disabled="disabled"
            v-model="form.questionInfo" placeholder="请输入问题描述" />
        </el-form-item>
        <el-form-item label="成因" prop="questionOrigin">
          <el-input :autosize="{ minRows: 3, maxRows: 10 }" maxlength="255" type="textarea" :disabled="disabled"
            v-model="form.questionOrigin" placeholder="请输入成因" />
        </el-form-item>
        <el-form-item label="规范依据">
          <el-input :autosize="{ minRows: 3, maxRows: 10 }" maxlength="255" type="textarea" :disabled="disabled"
            v-model="form.normativeBasis" placeholder="请输入规范依据" />
        </el-form-item>
        <el-form-item label="规范内容" >
          <el-input :autosize="{ minRows: 3, maxRows: 10 }" maxlength="255" type="textarea" :disabled="disabled"
            v-model="form.normativeInfo" placeholder="请输入规范内容" />
        </el-form-item>
        
        <el-form-item label="防治措施" prop="preventiveMeasures">
          <el-input :autosize="{ minRows: 3, maxRows: 10 }" maxlength="255" type="textarea" :disabled="disabled"
            v-model="form.preventiveMeasures" placeholder="请输入防治措施" />
        </el-form-item>
        <el-form-item label="质量照片" prop="picName">
          <div class="block" v-if="disabled == false">
            <ImageUpload v-model="form.picsArr" ref="imageUpload" @addNumData="addNumData" @delNumData="delNumData" ></ImageUpload>
          </div>
          <div class="block" v-else-if="disabled == true">
            <el-row>
              <div class="demo-image">
                <div class="block" v-for="(url,index) in form.picsArr" :key="index">
                  <el-col :span="6">
                    <el-image style="width: 150px; height: 150px" :src="url.url" :fit="fit"
                      :preview-src-list="form.pic"></el-image>
                  </el-col>
                </div>
              </div>
            </el-row>
          </div>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input :autosize="{ minRows: 3, maxRows: 10 }" maxlength="255" type="textarea" :disabled="disabled" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number :disabled="disabled" v-model="form.sort" :min="1" :max="100" label="排序"
            style="width: 305px"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!disabled">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  listQuestions,
  getQuestions,
  delQuestions,
  addQuestions,
  updateQuestions,
} from "@/api/quality/questions";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { treeselect } from "@/api/list/type";
export default {
  name: "questions",
  dicts: ["list_quality_level", "list_quality_question_source"],
  components: {
    Treeselect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问题规范库表格数据
      questionsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeNo: null,
        questionInfo: null,
        questionOrigin: null,
        preventiveMeasures: null,
        questionLevel: null,
        normativeBasis: null,
        normativeInfo: null,
        dataSource: null,
        pic: [],
        sort: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeNo: [
          { required: true, message: "参建单位不能为空", trigger: "blur" },
        ],
        questionLevel: [
          { required: true, message: "质量问题等级不能为空", trigger: "blur" }
        ],
        dataSource: [
          { required: true, message: "数据来源不能为空", trigger: "blur" }
        ],
        normativeBasis: [
          { required: true, message: "规范依据不能为空", trigger: "blur" }
        ],
        normativeInfo: [
          { required: true, message: "规范内容不能为空", trigger: "blur" }
        ],
        questionInfo: [
          { required: true, message: "质量问题不能为空", trigger: "blur" }
        ],
        questionOrigin: [
          { required: true, message: "成因不能为空", trigger: "blur" }
        ],
        sort: [{ required: true, message: "排序不能为空", trigger: "blur" }],
      },
      //查看
      disabled: false,
      // 部门树选项
      deptOptions: [],
      // 部门名称
      deptName: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
      typeNoData: {
        typeNo: null,
      },
      fit: 'contain',
      uploadList:[],
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  methods: {
    /** 查询管理清单列表 */
    getList() {
      this.loading = true;
      listQuestions(this.queryParams).then((response) => {
        this.questionsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      treeselect(this.typeNoData).then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.typeNo = data.id;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        typeNo: null,
        questionInfo: null,
        questionOrigin: null,
        preventiveMeasures: null,
        questionLevel: null,
        normativeBasis: null,
        normativeInfo: null,
        dataSource: null,
        pic: [],
        pics: null,
        remark: null,
        sort: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.typeNo = undefined;
      // this.$refs.tree.setCurrentKey(null);
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.typeNo = this.queryParams.typeNo;
      this.form.dataSource = "问题库提交";
      this.form.questionLevel = '1'
      this.disabled = false;
      this.open = true;
      this.title = "添加问题规范库";
    },
    // 查看
    LookUpdate(row) {
      const id = row.id || this.ids;
      getQuestions(id).then((response) => {
        this.form = response.data;
        if (this.form.picsArr!=null) {
          this.uploadList = this.form.pics.split(',')
          this.form.picsArr.forEach(item => item.url=process.env.VUE_APP_MINIO_BASE_API+ '/'+ item.bucket + "/" + item.name);
        }
        this.disabled = true;
        this.open = true;
        this.title = "问题规范库查看";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getQuestions(id).then((response) => {
        this.form = response.data;
        if (this.form.picsArr!=null) {
          this.uploadList = this.form.pics.split(',')
          this.form.picsArr.forEach(item => item.url=process.env.VUE_APP_MINIO_BASE_API+ '/'+ item.bucket + "/" + item.name);
        }
        this.disabled = false;
        this.open = true;
        this.title = "修改问题规范库";
      });
    },
    //修改状态
    switchData(row) {
      var data = {
        status: row.status,
        id: row.id,
      };
      updateQuestions(data).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      // const imageUploadRef = this.$refs.imageUpload;
      // const list = imageUploadRef.getFileList();
      // this.form.pic = list.map(item => item.url)
      this.form.pics = this.uploadList.toString()
      this.form.picsArr = null
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateQuestions(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQuestions(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除管理清单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delQuestions(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
     addNumData(data){
      this.uploadList.push(data.fileId);
    },
    delNumData(data){
      this.uploadList.splice(data, 1)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'quality/questions/export',
        {
          ...this.queryParams,
        },
        `questions_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.dialogForm {
  overflow-y: auto;
  overflow-x: hidden;
  max-height: 500px;
}
</style>
